package cn.lzit.request.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class CircleAreaServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        out.println("<html><body bgcolor='pink'>");

        String strRadius = request.getParameter("radius");
        try {
            double r = Double.parseDouble(strRadius);
            double area = Math.PI * r * r;
            double circumference = 2 * Math.PI * r;
            out.print("<br>半径: " + r);
            out.print("<br>圆面积: " + area);
            out.print("<br>圆周长: " + circumference);
            out.println("</body></html>");
        } catch (NumberFormatException e) {
            // 设置错误信息
            request.setAttribute("error", strRadius + ": 半径数据不合法");
            // 请求转发到错误处理 Servlet
            RequestDispatcher rd = request.getRequestDispatcher("/error");
            rd.forward(request, response);
        }
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    }
